package com.kgc.personprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.Good;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.personcommon.pojo.Department;
import com.kgc.personprovider.dao.DeptMapper;
import com.kgc.personprovider.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DeptServiceImpl implements DeptService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public ResponseMsg getAll(Map<String, Object> map) {
        logger.info("DeptServiceImpl getAll, map:{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        List<Department> list = deptMapper.getAll(map);
        logger.debug("deptMapper getAll, list:{}", list+", map:{}", map);
        if(list!=null&&list.size()>0){
            responseMsg.setCode("200");
            responseMsg.setData(list);
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getById(String id) {
        logger.info("DeptServiceImpl getById, id:{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        Department department =deptMapper.getById(id);
        logger.debug("deptMapper getById id:"+id+", department:{}", department);
        if(department!=null){
            responseMsg.setData(department);
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getByPage(Map<String, Object> map) {
        logger.info("DeptServiceImpl getByPage, map:{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        PageHelper.startPage(Integer.valueOf(map.get("pageNow").toString()),Integer.valueOf(map.get("pageSize").toString()));
        List<Department> list = deptMapper.getAll(map);
        logger.debug("deptMapper getAll, list:{}", list+", map:{}", map);
        PageHelper.clearPage();
        PageInfo<Department> pageInfo = new PageInfo<>();
        if(list!=null&&list.size()>0){
            pageInfo.setList(list);
            pageInfo.setPageNo(Integer.valueOf(map.get("pageNow").toString()));
            pageInfo.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
            pageInfo.setTotal(deptMapper.getCount(map));
        }
        if(pageInfo.getList()!=null&&pageInfo.getList().size()>0){
            responseMsg.setCode("200");
            responseMsg.setData(pageInfo);
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg add(Department department) {
        logger.info("deptServiceImpl add, department:{}", department);
        ResponseMsg responseMsg = new ResponseMsg();
        department.setCreateDate(new Date());
        department.setUpdateDate(new Date());
//        department.setCreateUser("1111");
//        department.setUpdateUser("1111");
        int count = deptMapper.add(department);
        logger.debug("deptMapper add, count:{}", count+", department:{}", department);
        if(count>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg update(Department department) {
        logger.info("deptServiceImpl update, department:{}", department);
        ResponseMsg responseMsg = new ResponseMsg();
        department.setUpdateDate(new Date());
//        department.setUpdateUser("222");
        int count = deptMapper.update(department);
        logger.debug("deptMapper update, count:{}", count+", department:{}", department);
        if(count>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg delete(String id) {
        logger.info("deptServiceImpl delete, id:{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("updateDate", new Date());
        map.put("updateUser", "123");
        int count = deptMapper.delete(id);
        logger.debug("deptMapper delete, count:{}", count+", id:{}", id);
        if(count>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }
}
